Skip to content

JS: Overhaul import resolution#19391

Merged
asgerf merged 26 commits intogithub:mainfrom
asgerf:js/typescript-path-resolution
May 13, 2025
Merged

JS: Overhaul import resolution#19391
asgerf merged 26 commits intogithub:mainfrom
asgerf:js/typescript-path-resolution

Conversation

@asgerf
Copy link
Contributor

@asgerf asgerf commented Apr 28, 2025

Updates JS import resolution to avoid depending on the TypeScript compiler, which ultimately led to a completely rewrite of import resolution.

Deprecating PathExpr

This PR deprecates PathExpr, PathString, and Path, in favor of the parameterised modules Folder::Resolve and ResolveExpr and extended models of tsconfig.json and package.json files.

PathExpr tried to represent multiple things simultaneously:

  • expressions that we need to resolve as a path
  • expressions that we need to constant-fold
  • expressions that we're able to constant-fold

The new implementation has a clear separation between these and can do more powerful constant folding as a result.

The old abstraction also could not properly support path mapping. It had a concept of "search root" but assumed that the value to resolve did not depend on the search root, which is not true for mappings like @/* -> src/*, where the @/ prefix must be chopped off.

It also forced all path-resolution logic to happen within a single layer, which complicated library models that need import resolution (Angular). Attempts to refactor things often led to negative recursion due to the Angular model. Parameterised modules don't have this problem as any model can resolve whatever paths they need in the layer they need it.

Evaluation

Evaluation shows neutral performance and about 5k new import edges, and about 30k new call edges, and a few lost imports and call as well.

The alert changes aren't so interesting. Some results were gained in a copy of jQuery, and two alerts were lost as we lost a library input, but not one that seemed likely to be user-controlled.

Review notes

Commit-by-commit view is strongly recommended.

A bunch of unit tests are added in a single commit. The annotations initially represent what we resolve using the baseline implementation (relying on the TypeScript compiler) and you may not want to worry too much about whether those initial annotation are correct; they just represent the baseline.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

JS no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants